package job

import (
	"blog_go/global"
	"blog_go/models"
	"blog_go/service/redis_ser"
	"github.com/sirupsen/logrus"
)

func (c commentCount) Run() {
	logrus.Info("执行评论定时任务")
	digg := redis_ser.NewCommentDigg()
	diggMap := digg.DiggGetAll()
	var list []commentCount
	// 这样只会查询这个结构体中的两个对象
	global.DB.Model(&models.CommentModel{}).Find(&list)
	redisDiggVal := make(map[uint]int)
	for _, info := range list {
		diggCount := diggMap[info.ID]
		redisDiggCount := info.DiggCount
		if diggCount > info.DiggCount {
			redisDiggCount = diggCount
			global.DB.Model(&models.CommentModel{}).Update("digg_count", diggCount)
		}
		redisDiggVal[info.ID] = redisDiggCount
	}
	digg.DiggSetAll(redisDiggVal)
	logrus.Info("评论定时任务执行结束")
}

type commentCount struct {
	ID        uint // id
	DiggCount int  // 点赞量
}
